package cn.deschen.app.controller;

import cn.deschen.app.vo.LoginSuccessVO;
import cn.deschen.biz.constants.UserConstants;
import cn.deschen.biz.domain.entity.CmUser;
import cn.deschen.biz.dto.user.UserAddDTO;
import cn.deschen.biz.dto.user.UserLoginDTO;
import cn.deschen.biz.service.CmUserService;
import cn.deschen.common.api.BizResCode;
import cn.deschen.common.api.CmRes;
import cn.deschen.common.config.security.JwtProperties;
import cn.deschen.common.utils.JwtTokenUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @Author hanbin_chen
 * @Description 用户相关接口
 * @DateTime 2021/6/3
 * @Version V1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/common/user")
@Api(tags = "用户模块")
public class CmUserController {

    @Autowired
    private CmUserService userService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private JwtTokenUtil tokenUtil;

    @Autowired
    private JwtProperties jwtProperties;

    @ApiOperation("创建用户")
    @PostMapping("/add")
    public CmRes<Void> add(@Valid @RequestBody UserAddDTO addDTO) {
        String encryptPwd = DigestUtils.md5Hex(addDTO.getPassword());
        CmUser user = mapperFacade.map(addDTO, CmUser.class);
        user.setPassword(encryptPwd);
        user.setAccountStatus(UserConstants.AccountStatus.NORMAL);
        userService.save(user);
        return CmRes.success();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CmRes<LoginSuccessVO> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        String encryptPwd = DigestUtils.md5Hex(loginDTO.getPassword());
        QueryWrapper<CmUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(CmUser::getUsername, loginDTO.getUserName());
        CmUser user = userService.getOne(wrapper);
        if (null == user || !user.getPassword().equals(encryptPwd)) {
            return CmRes.failure(BizResCode.USER_PWD_ERROR);
        }
        if (user.getAccountStatus().equals(UserConstants.AccountStatus.STOP)) {
            return CmRes.failure(BizResCode.USER_IS_STOP);
        }
        String token = tokenUtil.generateToken(user.getUsername());
        LoginSuccessVO loginSuccessVO = new LoginSuccessVO();
        loginSuccessVO.setAuthHeaderName("Authorization");
        loginSuccessVO.setToken(jwtProperties.getTokenHead() + token);
        loginSuccessVO.setTokenExpires(LocalDateTime.now().plus(jwtProperties.getExpiration(), ChronoUnit.SECONDS));
        return CmRes.success(loginSuccessVO);
    }
}


